package ui {
	import flash.geom.Point;
	import jazztures.geom.Point3D;
	import jazztures.objects.Skeleton;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class DollModeler extends Sprite
	{
		private var headSpr:BodyPart;
		private var neckSpr:BodyPart;
		
		private var leftShoulderSpr:BodyPart;
		private var leftElbowSpr:BodyPart;
		private var leftHandSpr:BodyPart;
		
		private var rightShoulderSpr:BodyPart;
		private var rightElbowSpr:BodyPart;
		private var rightHandSpr:BodyPart;
		private var stomachSpr:BodyPart;
		
		private var leftWaistSpr:BodyPart;
		private var leftKneeSpr:BodyPart;
		private var leftFootSpr:BodyPart;
		
		private var rightWaistSpr:BodyPart;
		private var rightKneeSpr:BodyPart;
		private var rightFootSpr:BodyPart;

		
		private var _width:Number;
		private var _height:Number;
		
		private var nowDragging:Sprite;
			
		public function DollModeler(p_width:Number, p_height:Number)
		{
			super();
			
			_width = p_width;
			_height = p_height;
			
			var center:Point = new Point(p_width/2, p_height/2);
			
			neckSpr = new BodyPart(10, 10);
			addChild(neckSpr);
			neckSpr.x = center.x;
			neckSpr.y = center.y-40;
			
			headSpr = new BodyPart(10, 10);
			addChild(headSpr);
			headSpr.x = center.x;
			headSpr.y = neckSpr.y - 30;
			
			stomachSpr = new BodyPart(10,10);
			addChild(stomachSpr);
			stomachSpr.x = center.x;
			stomachSpr.y = neckSpr.y + 30;
			
			leftShoulderSpr = new BodyPart(10, 10);
			addChild(leftShoulderSpr);
			leftShoulderSpr.x = neckSpr.x -30;
			leftShoulderSpr.y = neckSpr.y;
			
			leftElbowSpr = new BodyPart(10, 10);
			addChild(leftElbowSpr);
			leftElbowSpr.x = leftShoulderSpr.x - 30;
			leftElbowSpr.y = leftShoulderSpr.y;
			
			leftHandSpr = new BodyPart(10, 10);
			addChild(leftHandSpr);
			leftHandSpr.x = leftElbowSpr.x -30;
			leftHandSpr.y = leftElbow.y;
			
			rightShoulderSpr = new BodyPart(10, 10);
			addChild(rightShoulderSpr);
			rightShoulderSpr.x = neckSpr.x +30;
			rightShoulderSpr.y = neckSpr.y;
			
			rightElbowSpr = new BodyPart(10, 10);
			addChild(rightElbowSpr);
			rightElbowSpr.x = rightShoulderSpr.x + 30;
			rightElbowSpr.y = rightShoulderSpr.y;
			
			rightHandSpr = new BodyPart(10, 10);
			addChild(rightHandSpr);
			rightHandSpr.x = rightElbowSpr.x + 30;
			rightHandSpr.y = rightElbow.y;
			
			leftWaistSpr = new BodyPart(10,10);
			addChild(leftWaistSpr);
			leftWaistSpr.x = stomachSpr.x - 30;
			leftWaistSpr.y = stomachSpr.y + 30;
			
			leftKneeSpr = new BodyPart(10,10);
			addChild(leftKneeSpr);
			leftKneeSpr.x = leftWaistSpr.x;
			leftKneeSpr.y = leftWaistSpr.y + 30;
			
			leftFootSpr = new BodyPart(10,10);
			addChild(leftFootSpr);
			leftFootSpr.x = leftKneeSpr.x;
			leftFootSpr.y = leftKneeSpr.y + 30;
			
			rightWaistSpr = new BodyPart(10,10);
			addChild(rightWaistSpr);
			rightWaistSpr.x = stomachSpr.x + 30;
			rightWaistSpr.y = stomachSpr.y + 30;
			
			rightKneeSpr = new BodyPart(10,10);
			addChild(rightKneeSpr);
			rightKneeSpr.x = rightWaistSpr.x;
			rightKneeSpr.y = rightWaistSpr.y + 30;
			
			rightFootSpr = new BodyPart(10,10);
			addChild(rightFootSpr);
			rightFootSpr.x = rightKneeSpr.x;
			rightFootSpr.y = rightKneeSpr.y + 30;
			
			setEvents(headSpr);
			setEvents(neckSpr);
			setEvents(stomachSpr);
			
			setEvents(leftShoulderSpr);
			setEvents(leftElbowSpr);
			setEvents(leftHandSpr);
			
			setEvents(rightShoulderSpr);
			setEvents(rightElbowSpr);
			setEvents(rightHandSpr);
			
			setEvents(leftWaistSpr);
			setEvents(leftKneeSpr);
			setEvents(leftFootSpr);
			
			setEvents(rightWaistSpr);
			setEvents(rightKneeSpr);
			setEvents(rightFootSpr);
		}
		
		private function setEvents(part:BodyPart):void{
			part.addEventListener(MouseEvent.MOUSE_DOWN, mouseListener);
			part.addEventListener(MouseEvent.MOUSE_WHEEL, mouseListener);
		}
		
		private function mouseListener(evt:MouseEvent):void{
			switch(evt.type){
				case MouseEvent.MOUSE_DOWN:
					Sprite(evt.currentTarget).startDrag(false, new Rectangle(0,0,_width, _height));
					nowDragging = Sprite(evt.currentTarget);
					stage.addEventListener(MouseEvent.MOUSE_UP, mouseListener);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseListener);
					break;
				
				case MouseEvent.MOUSE_UP:
					nowDragging.stopDrag();
					stage.removeEventListener(MouseEvent.MOUSE_UP, mouseListener);
					stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseListener);
					break;
				
				case MouseEvent.MOUSE_MOVE:
					update();
					break;
				
				case MouseEvent.MOUSE_WHEEL:
					BodyPart(evt.currentTarget).z += evt.delta;
					update();
					break;
			}
		}
		
		private function update():void{
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get head():Point3D{
			return new Point3D(headSpr.x, headSpr.y , headSpr.z);
		}
		
		public function get neck():Point3D{
			return new Point3D(neckSpr.x, neckSpr.y, neckSpr.z);
		}
		
		public function get torso():Point3D{
			return new Point3D(stomachSpr.x , stomachSpr.y , stomachSpr.z);
		}
		
		public function get leftShoulder():Point3D{
			return new Point3D(leftShoulderSpr.x , leftShoulderSpr.y , leftShoulderSpr.z);
		}
		
		public function get leftElbow():Point3D{
			return new Point3D(leftElbowSpr.x , leftElbowSpr.y , leftElbowSpr.z);
		}
		
		public function get leftHand():Point3D{
			return new Point3D(leftHandSpr.x , leftHandSpr.y , leftHandSpr.z);
		}
		
		public function get rightShoulder():Point3D{
			return new Point3D(rightShoulderSpr.x , rightShoulderSpr.y  , rightShoulderSpr.z);
		}
		
		public function get rightElbow():Point3D{
			return new Point3D(rightElbowSpr.x , rightElbowSpr.y , rightElbowSpr.z);
		}
				
		public function get rightHand():Point3D{
			return new Point3D(rightHandSpr.x , rightHandSpr.y , rightHandSpr.z);
		}
		
		public function get leftHip():Point3D{
			return new Point3D(leftWaistSpr.x , leftWaistSpr.y , leftWaistSpr.z);
		}
		
		public function get leftKnee():Point3D{
			return new Point3D(leftKneeSpr.x , leftKneeSpr.y , leftKneeSpr.z);
		}
		
		public function get leftFoot():Point3D{
			return new Point3D(leftFootSpr.x , leftFootSpr.y , leftFootSpr.z);
		}
		
		public function get rightHip():Point3D{
			return new Point3D(rightWaistSpr.x , rightWaistSpr.y , rightWaistSpr.z);
		}
		
		public function get rightKnee():Point3D{
			return new Point3D(rightKneeSpr.x , rightKneeSpr.y , rightKneeSpr.z);
		}
		
		public function get rightFoot():Point3D{
			return new Point3D(rightFootSpr.x , rightFootSpr.y , rightFootSpr.z);
		}
		
		public function get skeleton():Skeleton{
			return new Skeleton(1, convert(head), convert(neck), convert(torso), convert(leftShoulder), convert(rightShoulder), 
				convert(leftHip), convert(rightHip), convert(leftKnee), convert(rightKnee), convert(leftFoot), convert(rightFoot), 
				convert(leftElbow), convert(rightElbow), convert(leftHand), convert(rightHand));
		}
		
		private function convert(point:Point3D):Object{
			var obj:Object = {x:point.x, y:point.y, z:point.z, orientationx:point.rotationX, orientationy:point.rotationY, orientationz:point.rotationZ};
			return obj;
		}
	}
}
import sweatless.graphics.SmartRectangle;

import flash.display.Sprite;

internal class BodyPart extends Sprite{
	
	private var _width:int;
	private var _height:int;
	
	private var _z:int;
	
	private var innerRect:SmartRectangle;
	private var outerRect:SmartRectangle;
	
	public function BodyPart(p_width:int=50, p_height:int=50){
		_width = p_width;
		_height = p_height;
		
		innerRect = new SmartRectangle(p_width, p_height);
		innerRect.x = -innerRect.width/2;
		innerRect.y = -innerRect.height/2;
		innerRect.colors =[0xFFFFFF];
		addChild(innerRect);
		
		outerRect = new SmartRectangle(p_width +10, p_height + 10);
		outerRect.x = -outerRect.width/2;
		outerRect.y = -outerRect.height/2;
		outerRect.colors = [0xFFFFFF];
		outerRect.alphas = [.1];
		outerRect.strokeColor = 0xFFFFFF;
		outerRect.stroke = true;
		addChild(outerRect);
		
		buttonMode = true;
		mouseChildren = false;
	}
	
	override public function set z(value:Number):void{
		_z = value > 100 ? 100 : value < -100 ? -100 : value;
		innerRect.width = _width - (_z/10);
		innerRect.height = _height - (_z/10);
		innerRect.x = -innerRect.width/2;
		innerRect.y = -innerRect.height/2;
	}
	
	override public function get z():Number{
		return _z;
	}
}